package top.kaoshanji.p2ch16a;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 代码清单-p2ch16a-470页a：AtomicInteger的应用示例
 * author kaoshanji
 * ^_^
 * create time 2025/2/26 16:12
 */
public class AtomicIntegerDemo {

    private static AtomicInteger counter = new AtomicInteger(0);

    static class Visitor extends Thread {

        @Override
        public void run() {
            for (int i = 0; i < 1000; i++) {
                counter.incrementAndGet();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {

        int num = 100;
        Thread[] threads = new Thread[num];
        for (int i = 0; i < num; i++) {
            threads[i] = new Visitor();
            threads[i].start();
        }

        for (int i = 0; i < num; i++) {
            threads[i].join();
        }

        System.out.println(counter.get());

    }

}
